package com.angel.architecture.flex.filter;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;

import com.angel.architecture.exceptions.NonBusinessException;
import com.angel.architecture.flex.locator.ObjectLocator;

/**
 * <p>La idea de este objeto es hacer el enganche de filtros instanciados con
 * Spring para una configuracion mas cheta</p>
 *
 * @user Juan Isern
 */
public class ProxyFilter implements Filter {
    private Filter targetFilter;

    public void init(FilterConfig filterConfig) throws ServletException {
        String targetBean = filterConfig.getInitParameter("targetBean");
        if (targetBean == null) {
            throw new NonBusinessException("Debe especificar un valor para la clave 'targetBean'");
        }
        targetFilter = ObjectLocator.getById(targetBean);
    }

    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        targetFilter.doFilter(servletRequest, servletResponse, filterChain);
    }

    public void destroy() {
    }
}
